package cn.tedu.knows.sys.security;

import cn.tedu.knows.sys.interceptor.AuthInterceptor;
import cn.tedu.knows.sys.interceptor.DemoInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }
    //从Spring容器中获得拦截器对象
    @Autowired
    private DemoInterceptor demoInterceptor;
    @Autowired
    private AuthInterceptor authInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInterceptor)
                //当访问url为/v1/auth/demo当前拦截器生效
                .addPathPatterns("");
        registry.addInterceptor(authInterceptor)
                .addPathPatterns(
                        "/v1/home",
                        "/v1/users/me"
                );
    }
}
